<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class Test extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('test')
            ->setDescription('the test command');        
    }

    protected function execute(Input $input, Output $output)
    {

        $url = 'http://abc.lsbgc68.cn/e08476hy9iz.mp4';
        // 对URL进行解析
        $parse = parse_url($url);
        // 设置有效时间为一个小时
        $time = time()+600;
        // 对有效时间进行16进制转换
        $T=dechex($time);
        // 七牛提供的 key
        $key="huangbin1991huangbin1991";
        // 按规定格式拼接字符串
        $S=$key.$parse['path'].$T;
        // 进行md5加密并进行小写转换
        $sign =strtolower(md5($S));
        // 拼接URL
        if(isset($parse['query'])){

            $url = $url."&sign=".$sign."&t=".$T;

        } else {

            $url = $url."?sign=" . $sign . "&t=" . $T;
        }
        dump($url);die;

        //$res = $this->send_msg();
 
    }

    public function getAntiLeechAccessUrlBasedOnTimestamp($url)
    {
        try{

            // 对URL进行解析
            $parse = parse_url($url);

            // 设置有效时间为一个小时
            $time = time()+3600;

            // 对有效时间进行16进制转换
            $T=dechex($time);

            // 七牛提供的 key
            $key="12345678";

            // 按规定格式拼接字符串
            $S=$key.$parse['path'].$T;

            // 进行md5加密并进行小写转换
            $sign =strtolower(md5($S));

            // 拼接URL
            if(isset($parse['query'])){

                $url = $url."&sign=".$sign."&t=".$T;

            } else {

                $url = $url."?sign=" . $sign . "&t=" . $T;
            }

            // 返回
            return $url;

        } catch(\Exception $e){

            // 输出异常信息
            echo $e->getMessage();
        }
    }


    //swoole客服端接口
    function send_msg(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);

        $key = 'local_test_'.date('Ymdhis');
        for ($i=0;$i<10000;$i++){
            $order_num = '4200000538'.date('Ymdhist').rand(1000000,9999999);
            $redis->lPush($key,$order_num);
        }

        $client = new Swoole\Client(SWOOLE_SOCK_TCP);
        if (!$client->connect('127.0.0.1', 9501, -1)) {
            exit("connect failed. Error: {$client->errCode}\n");
        }
        $client->send($key);
        $res = $client->recv();
        $client->close();
        return $res;
    }


    //curl_get 请求
    public function curlGet($url){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }

    }

    //curl_post 请求
    public function curlPost($url,$param,$post_file=false){
        $oCurl = curl_init();
        if(stripos($url,"https://")!==FALSE){
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (PHP_VERSION_ID >= 50500 && class_exists('\CURLFile')) {
            $is_curlFile = true;
        } else {
            $is_curlFile = false;
            if (defined('CURLOPT_SAFE_UPLOAD')) {
                curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false);
            }
        }
        if (is_string($param)) {
            $strPOST = $param;
        }elseif($post_file) {
            if($is_curlFile) {
                foreach ($param as $key => $val) {
                    if (substr($val, 0, 1) == '@') {
                        $param[$key] = new \CURLFile(realpath(substr($val,1)));
                    }
                }
            }
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach($param as $key=>$val){
                $aPOST[] = $key."=".urlencode($val);
            }
            $strPOST =  join("&", $aPOST);
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($oCurl, CURLOPT_POST,true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if(intval($aStatus["http_code"])==200){
            return $sContent;
        }else{
            return false;
        }
    }



}
